package com.jiuzhe.commercial.main;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.base.BaseActivity;
import com.jiuzhe.commercial.bean.RegisterBean;
import com.jiuzhe.commercial.constant.Constants;
import com.jiuzhe.commercial.http.CallBack;
import com.jiuzhe.commercial.http.HttpClient;
import com.jiuzhe.commercial.utils.UIUtils;
import com.jiuzhe.commercial.view.CustomProgressDialog;
import com.jungly.gridpasswordview.GridPasswordView;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by huanghao on 2018/5/25.
 */

public class SettingTwoPassWordActivity extends BaseActivity {

    @BindView(R.id.pswView)
    GridPasswordView pswView;
    private CustomProgressDialog customProgressDialog;
    private CustomProgressDialog cusDialog;
    private String mPassWord;
    private String mEndPsw;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_setting_two_password);
        setTitle("确认提现支付密码");
    }

    @Override
    protected void loadData() {
        pswView.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
            @Override
            public void onTextChanged(String psw) {

            }

            @Override
            public void onInputFinish(String psw) {
                mEndPsw = psw;
            }
        });
    }

    @OnClick({R.id.bt_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_next:
                submit();
                break;
        }
    }

    private void submit() {
        String psw = pswView.getPassWord();
        if (TextUtils.isEmpty(psw)){
            UIUtils.showToastSafe("请再次输入提现密码");
            return;
        }
        inPutPassWord();
    }

    private void inPutPassWord() {
        RegisterBean registerBean = (RegisterBean) getIntent().getSerializableExtra("register");
        mPassWord = getIntent().getStringExtra("psw");
        if (!mEndPsw.equals(mPassWord)){
            UIUtils.showToastSafe("两次输入的密码不一致");
            return;
        }
        dialog.show();
        HashMap<String, String> map = new HashMap<>();
        map.put("companyName", registerBean.getCompanyName());
        map.put("idCard", registerBean.getIdCard());
        map.put("phone", registerBean.getPhone());
        map.put("realName", registerBean.getRealName());
        map.put("idPhotoPersonUrl", registerBean.getIdPhotoPersonUrl());
        map.put("idPhotoUrl", registerBean.getIdPhotoUrl());
        map.put("telephone", registerBean.getTelephone());
        String str = UIUtils.Md5(mEndPsw);
        String jiamiStr = UIUtils.Md5(str);
        map.put("paymentCode", jiamiStr);

        HttpClient.post(this, Constants.REGISTER, map, new CallBack<String>() {
            @Override
            public void onSuccess(String s) {
                dialog.dismiss();
                show();
            }

            @Override
            public void onFailure(int errorType, String message) {
                super.onFailure(errorType, message);
                dialog.dismiss();
            }
        });
    }


    /**
     * 弹出提示框
     */
    private void show() {
        customProgressDialog = new CustomProgressDialog(this);
        cusDialog = customProgressDialog.createDialog(this);
        Button btOk = cusDialog.findViewById(R.id.bt_ok);
        btOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                cusDialog.dismiss();
                Intent intent  = new Intent(SettingTwoPassWordActivity.this, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (customProgressDialog != null)
            customProgressDialog.onDestroy();
        dialog = null;
    }
}
